package com.example.rpc_easy.servers;

import com.example.rpc_easy.Serializable.JdkSerializable;
import com.example.rpc_easy.Serializable.Serializable1;
import com.example.rpc_easy.model.RpcRequestExample;
import com.example.rpc_easy.model.RpcResponseExample;
import com.example.rpc_easy.register.LocalRegister;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class HttpServerHandle implements Handler<HttpServerRequest>  {
    @Override
    public void handle(HttpServerRequest httpServerRequest) {

       final Serializable1 serializable1 = new JdkSerializable();

       httpServerRequest.bodyHandler(body->{
           //反序列化请求为对象
           byte[] aByte = body.getBytes();
           RpcRequestExample rpcRequestExample =null;
           try {
               rpcRequestExample= serializable1.deserialize(aByte, RpcRequestExample.class);
//               System.out.println(rpcRequestExample.getServiceName());
//               System.out.println(rpcRequestExample.getMethodName());
//               System.out.println(rpcRequestExample.getArgs());
           } catch (IOException e) {
               e.printStackTrace();
           }
           //System.out.println("反序列化结果: " + rpcRequestExample);
            //构造相应结果
           RpcResponseExample rpcResponseExample = new RpcResponseExample();
           if (rpcRequestExample==null){
               httpServerRequest.response().end("请求为空");
               MyResponse(httpServerRequest,rpcResponseExample, serializable1);
              // return;
           }
           try {
               // 反射获取服务实现类
               Class<?> aClass = LocalRegister.get(rpcRequestExample.getServiceName());
               Method method = aClass.getMethod(rpcRequestExample.getMethodName()
                       ,rpcRequestExample.getParameterTypes());
               Object invoke = method.invoke(aClass.newInstance(), rpcRequestExample.getArgs());
               //封装相应类
               rpcResponseExample.setData(invoke);
               rpcResponseExample.setDataType(method.getReturnType());
               rpcResponseExample.setMessage("success");
           } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
               throw new RuntimeException(e);
           }
           //封装响应
           MyResponse(httpServerRequest,rpcResponseExample, serializable1);
       });




    }
    void  MyResponse(HttpServerRequest httpServerRequest,
                     RpcResponseExample rpcResponseExample,
                     Serializable1 serializable1)  {
        HttpServerResponse serverResponse = httpServerRequest.response();
        serverResponse.putHeader("content-type","application/json");

        try {
            byte[] serialize = serializable1.serialize(rpcResponseExample);
            serverResponse.end(Buffer.buffer(serialize));
        } catch (IOException e) {
            e.printStackTrace();
            serverResponse.end(Buffer.buffer());
        }


    }
}
